package com.obas.components{   
	import com.ease.core.components.PerspectiveBox;
   import com.ease.core.events.PerspectiveEvent;
   import com.obas.model.visualobject.PerspectiveToggleItem;
   
   import mx.binding.utils.BindingUtils;
   import mx.collections.ArrayCollection;
   import mx.containers.HBox;
   import mx.controls.Button;
   import mx.controls.ToggleButtonBar;
   import mx.events.FlexEvent;
   import mx.events.ItemClickEvent;
   import mx.events.MenuEvent;

   [Event(name="{com.ease.core.events.PerspectiveEvent.REQUEST_ID}", type="com.ease.core.events.PerspectiveEvent")]
   public class AppPerspectiveBox extends PerspectiveBox {
      [Bindable]
      public var perspectiveItems:ArrayCollection;

      private var _selectedPerspective:PerspectiveToggleItem;

      private var _labelFunction:Function = null;
      private var _labelFunctionChange:Boolean = false;
      public function get labelFunction():Function {
         return _labelFunction;
      }
      public function set labelFunction(value:Function):void {
         _labelFunction = value;
         _labelFunctionChange = true;
         invalidateProperties();
      }

      private var _iconField:String = null;
      private var _iconFieldChange:Boolean = false;
      public function get iconField():String {
         return _iconField;
      }
      public function set iconField(value:String):void {
         _iconField = value;
         _iconFieldChange = true;
         invalidateProperties();
      }

      override protected function createChildren():void {
         super.createChildren();
      }

      override protected function onCreationComplete(event:FlexEvent):void {
         BindingUtils.bindProperty(perspectiveSwitcher, "dataProvider", this, "perspectiveItems");
         BindingUtils.bindProperty(popupButton, "dataProvider", this, "perspectiveItems");
         BindingUtils.bindProperty(perspectiveBar, "dataProvider", this, "perspectiveItems");
      }

      override protected function onPerspectiveToggleButtonBarClick(event:ItemClickEvent):void {
         var item:PerspectiveToggleItem = event.item as PerspectiveToggleItem;
         var perspectiveId:String = item.perspectiveId;
         if (!_selectedPerspective || _selectedPerspective.perspectiveId != perspectiveId) {
            var perspectiveEvent:PerspectiveEvent = new PerspectiveEvent(perspectiveId, null);
            dispatchEvent(perspectiveEvent);
         }
      }
      
      override protected function onPerspectiveMenuClick(event:MenuEvent):void {
         var item:PerspectiveToggleItem = event.item as PerspectiveToggleItem;
         var perspectiveId:String = item.perspectiveId;
         if (!_selectedPerspective || _selectedPerspective.perspectiveId != perspectiveId) {
            var perspectiveEvent:PerspectiveEvent = new PerspectiveEvent(perspectiveId, null);
            dispatchEvent(perspectiveEvent);
         }
      }

      [EventHandler("{com.ease.core.events.PerspectiveEvent.REQUEST_ID}")]
      public function onPerspectiveEvent(event:PerspectiveEvent):void {
         if (event.actionType == PerspectiveEvent.TYPE_OPENED) {
            var len:int = perspectiveItems.length;
            for (var ii:int = len - 1; ii >= 0; ii --) {
               var item:PerspectiveToggleItem = perspectiveItems[ii];
               if (event.perspectiveId == item.perspectiveId) {
                  perspectiveBar.selectedIndex = ii;
                  _selectedPerspective = item;
                  break;
               }
            }
         }
      }

      override protected function commitProperties():void {
         super.commitProperties();
         if (_labelFunctionChange) {
            perspectiveBar.labelFunction = _labelFunction;
            _labelFunctionChange = false;
         }
         if (_iconFieldChange) {
            perspectiveBar.iconField = _iconField;
            perspectiveSwitcher.iconField = _iconField;
            popupButton.iconField = _iconField;
            _iconFieldChange = false;
         }
      }
   }
}